<?php
	  	/**
			Here, it will print each primary UL based on whats in the Categories table.
			After each LI (each category), it will print the subcategories for that main category,
			based on what matches its parentID in the subcategories1 table. If the subcategory has no
			subcategories, then it will be a link. If not, after each subcategory, it will call a function
			to print each subcategory, based on the parentID match in subcategories2. Each of these subcategories
			must be links, as they are the final subcategory level.
		*/
	  ?>
	  	<ul id="ddsubmenu1" class="ddsubmenustyle">
		<?php
			$SQL = "SELECT * FROM categories ORDER BY catID";
			$query = mysql_query($SQL, $db);
			while($data = mysql_fetch_array($query))
			{
				$value = $data["catID"];

				if($lg == "US")
				{
					$name = $data["name_en"];
				}
				else
				{
					$name = $data["name_fr"];
				}
				echo "<li><a style=\"cursor:pointer;\">$name</a>";
				printSubCat1($value, $db, $lg);
				echo "</li>";
			}
		?>
	 	</ul>
		<?php
			/**
				Uses the main category id to print the subcategory level.
			*/
			function printSubCat1($id, $db, $lg)
			{
				$SQL = "SELECT * FROM subCategories1 WHERE parentID =" . $id . " ORDER BY hasChild DESC, subCatID";
				$result = mysql_query($SQL, $db);
				echo "<ul>"; //start the subcategory
				while($data = mysql_fetch_array($result))
				{
					$value = $data["subCatID"];
					if($lg == "US")
					{
						$name = $data["name_en"];
					}
					else
					{
						$name = $data["name_fr"];
					}
					
					if($data["hasChild"] == "Y")
					{
						echo "<li><a style=\"cursor:pointer;\">$name</a>";
						printSubCat2($value, $db, $lg);
					}
					else
					{
						echo "<li><a href=\"index.php?a=2&lg=$lg&subcat=$value\">$name</a>";
					}
					echo "</li>";
				}
				echo "</ul>";
			}//end function
			
			/**
				Uses the subcategory id to print the subcategories under the subcategory.
			*/
			function printSubCat2($id, $db, $lg)
			{
				$SQL = "SELECT * FROM subCategories2 WHERE parentID =" . $id . " ORDER BY subCatID";
				$result = mysql_query($SQL, $db);
				echo "<ul>"; //start the subcategory
				while($data = mysql_fetch_array($result))
				{
					$value = $data["subCatID"];
					if($lg == "US")
					{
						$name = $data["name_en"];
					}
					else
					{
						$name = $data["name_fr"];
					}
					echo "<li><a href=\"index.php?a=2&lg=$lg&cat=$value\">$name</a>";
					echo "</li>";
				}
				echo "</ul>";
			}//end function
		?>